import { Router } from 'vue-router';
import { useUserStoreWithOut } from '/@/store/user';
import { PageEnum } from '/@/enums/pageEnum';

export function createPermissionGuard(router: Router) {
  const userStore = useUserStoreWithOut();
  router.beforeEach(async (to, from, next) => {
    const token = userStore.getToken;
    if (to.path == PageEnum.BASE_LOGIN) {
      next();
      return;
    }

    if (!token) {
      const redirectData: {
        path: string;
        replace: boolean;
        query?: Recordable<string>;
      } = {
        path: PageEnum.BASE_LOGIN,
        replace: true,
      };
      next(redirectData);
      return;
    }
    next();
    return;
  });
}
